package practice.seqList;

import java.util.Arrays;

class SeqList implements ISeqList {
    public int[] array;
    public int useSize;

    public SeqList() {
        this.array=new int[10];
    }

    public SeqList(int initCapacity) {
        this.array=new int[initCapacity];
    }
    private boolean isFull(){
        return array.length==useSize;
    }
    // 新增元素,默认在数组最后新增
    public void add(int val) {
        if (isFull()){
            array= Arrays.copyOf(array,array.length*2);
        }
        array[useSize]=val;
        useSize++;
    }
    private void isIndexLegal (int pos)throws IndexNotLegalException {
        if (pos>=useSize+1||pos<0){
            throw new IndexNotLegalException("下标异常");
        }
    }
    // 在 pos 位置新增元素
    public void add( int pos,int val) {
        try {
        isIndexLegal(pos);
        }catch (Exception e){
            e.printStackTrace();
        }
        if (isFull()){
            array= Arrays.copyOf(array,array.length*2);
        }
            int cur = useSize;
            for (int i = useSize - pos; i > 0; i--) {

                array[cur] = array[cur - 1];
                cur--;
            }
            array[pos] = val;
            useSize++;

    }
    // 判定是否包含某个元素
    public boolean contains(int key) {
        for (int i = 0; i < useSize; i++) {
            if (array[i]==key){
                return true;
            }
        }
        return false;
    }
    // 查找某个元素对应的位置
    public int indexOf(int toFind) {
        for (int i = 0; i < useSize; i++) {
            if (array[i]==toFind){
                return i;
            }
        }
        return -1;
    }
    private void checkIndexGetSet(int pos)throws IndexNotLegalException{
        if (pos<0||pos>=useSize){
            throw new IndexNotLegalException("GetSet下标错误");
        }
    }
    // 获取 pos 位置的元素
    public int get(int pos) {
        try {
            checkIndexGetSet(pos);
        }catch (Exception e){
            e.printStackTrace();
        }
        return array[pos];
    }
    // 给 pos 位置的元素设为 value
    public void set(int pos, int value) {
        try {
            checkIndexGetSet(pos);
        }catch (Exception e){
            e.printStackTrace();
        }
         array[pos]=value;
    }
    //删除第一次出现的关键字key
    public void remove(int toRemove) {

        for (int i = 0; i < useSize; i++) {
            if (array[i]==toRemove){
                int tmp=i;
                for (int j = tmp; j <=useSize-2; j++) {
                    array[j]=array[j+1];
                }
                useSize--;
                break;
            }
        }

    }
    public void removeAll(int toRemove) {
        while (contains(toRemove)) {
            for (int i = 0; i < useSize; i++) {
                if (array[i] == toRemove) {
                    int tmp = i;
                    for (int j = tmp; j <= useSize - 2; j++) {
                        array[j] = array[j + 1];
                    }
                    useSize--;
                    break;
                }
            }
        }

    }
    // 获取顺序表长度
    public int size() {
        return useSize;
    }
    // 清空顺序表
    public void clear() {
        useSize=0;
    }
    private boolean isEmpty(){
        return useSize==0;
    }
    // 打印顺序表，注意：该方法并不是顺序表中的方法，为了方便看测试结果给出的
    public void display() {
        if (isEmpty()){
            return;
        }
        for (int i = 0; i < useSize; i++) {
            System.out.print(array[i]+" ");
        }
        System.out.println();
    }
}
public class Text1 {
    public static void main(String[] args) {
        ISeqList iSeqList=new SeqList();
        iSeqList.add(99);
        iSeqList.add(99);
        iSeqList.add(99);
        iSeqList.add(99);
        iSeqList.display();

        iSeqList.add(4,99);
        iSeqList.display();

        iSeqList.add(4,99);
        iSeqList.display();

        iSeqList.add(3,99);
        iSeqList.display();
//        System.out.println(iSeqList.contains(1));
//        System.out.println(iSeqList.indexOf(100));
//        System.out.println(iSeqList.get(1));
//        iSeqList.set(5,100);

        System.out.println("==========");
        iSeqList.removeAll(99);
        //iSeqList.clear();

        iSeqList.display();
        System.out.println(iSeqList.size());

    }
}
